package cn.kent.androidtrivia

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import cn.kent.androidtrivia.databinding.FragmentGameBinding
import kotlin.math.min

class GameFragment : Fragment() {

    data class Question(val text: String, val answers: List<String>)

    private val questions: MutableList<Question> = mutableListOf(
        Question(
            "What is Android Jetpack?",
            listOf("All of these", "Tools", "Documentation", "Libraries")
        ),
        Question(
            "What is the base class for layouts?",
            listOf("ViewGroup", "ViewSet", "ViewCollection", "ViewRoot")
        ),
        Question(
            "What layout do you use for complex screens?",
            listOf("ConstraintLayout", "GridLayout", "LinearLayout", "FrameLayout")
        ),
        Question(
            "What do you use to push structured data into a layout?",
            listOf("Data binding", "Data pushing", "Set text", "An OnClick method")
        ),
        Question(
            "What method do you use to inflate layouts in fragments?",
            listOf("onCreateView()", "onActivityCreated()", "onCreateLayout()", "onInflateLayout()")
        ),
        Question(
            "What's the build system for Android?",
            listOf("Gradle", "Graddle", "Grodle", "Groyle")
        ),
        Question(
            "Which class do you use to create a vector drawable?",
            listOf("VectorDrawable", "AndroidVectorDrawable", "DrawableVector", "AndroidVector")
        ),
        Question(
            "Which one of these is an Android navigation component?",
            listOf("NavController", "NavCentral", "NavMaster", "NavSwitcher")
        ),
        Question(
            "Which XML element lets you register an activity with the launcher activity?",
            listOf("intent-filter", "app-registry", "launcher-registry", "app-launcher")
        ),
        Question(
            "What do you use to mark a layout for data binding?",
            listOf("<layout>", "<binding>", "<data-binding>", "<dbinding>")
        )
    )

    lateinit var currentQuestion: Question
    lateinit var answers: MutableList<String>
    private var questionIndex = 0
    private val numQuestions = min((questions.size + 1) / 2, 3)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = DataBindingUtil.inflate<FragmentGameBinding>(
            inflater,
            R.layout.fragment_game,
            container,
            false
        )

        randomizeQuestions()

        binding.game = this
        binding.submitButton.setOnClickListener { v: View ->
            val checkedId = binding.questionRadioGroup.checkedRadioButtonId
            if (checkedId != -1) {
                var answerIndex = 0
                when (checkedId) {
                    R.id.secondAnswerRadioButton -> answerIndex = 1
                    R.id.thirdAnswerRadioButton -> answerIndex = 2
                    R.id.fourthAnswerRadioButton -> answerIndex = 3
                }
                if (answers[answerIndex] == currentQuestion.answers[0]) {
                    questionIndex++
                    if (questionIndex < numQuestions) {
                        currentQuestion = questions[questionIndex]
                        setQuestions()
                        binding.invalidateAll()
                    } else {
                        v.findNavController().navigate(
                            GameFragmentDirections.actionGameFragmentToGameWonFragment(
                                numQuestions,
                                questionIndex
                            )
                        )
                    }
                } else {
                    v.findNavController()
                        .navigate(GameFragmentDirections.actionGameFragmentToGameOverFragment())
                }
            }
        }
        return binding.root
    }

    private fun randomizeQuestions() {
        questions.shuffle()
        questionIndex = 0
        setQuestions()
    }

    private fun setQuestions() {
        currentQuestion = questions[questionIndex]
        answers = currentQuestion.answers.toMutableList()
        answers.shuffle()
        if (activity is AppCompatActivity) {
            (activity as AppCompatActivity).supportActionBar?.title =
                getString(R.string.title_android_trivia_question, questionIndex + 1, numQuestions)
        }
    }
}
